{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 13.13 \u5b9e\u73b0\u4e00\u4e2a\u8ba1\u65f6\u5668\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bb0\u5f55\u7a0b\u5e8f\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u6240\u82b1\u8d39\u7684\u65f6\u95f4"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "time \u6a21\u5757\u5305\u542b\u5f88\u591a\u51fd\u6570\u6765\u6267\u884c\u8ddf\u65f6\u95f4\u6709\u5173\u7684\u51fd\u6570\u3002\n\u5c3d\u7ba1\u5982\u6b64\uff0c\u901a\u5e38\u6211\u4eec\u4f1a\u5728\u6b64\u57fa\u7840\u4e4b\u4e0a\u6784\u9020\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u7684\u63a5\u53e3\u6765\u6a21\u62df\u4e00\u4e2a\u8ba1\u65f6\u5668\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import time\n\nclass Timer:\n    def __init__(self, func=time.perf_counter):\n        self.elapsed = 0.0\n        self._func = func\n        self._start = None\n\n    def start(self):\n        if self._start is not None:\n            raise RuntimeError('Already started')\n        self._start = self._func()\n\n    def stop(self):\n        if self._start is None:\n            raise RuntimeError('Not started')\n        end = self._func()\n        self.elapsed += end - self._start\n        self._start = None\n\n    def reset(self):\n        self.elapsed = 0.0\n\n    @property\n    def running(self):\n        return self._start is not None\n\n    def __enter__(self):\n        self.start()\n        return self\n\n    def __exit__(self, *args):\n        self.stop()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53ef\u4ee5\u88ab\u7528\u6237\u6839\u636e\u9700\u8981\u542f\u52a8\u3001\u505c\u6b62\u548c\u91cd\u7f6e\u7684\u8ba1\u65f6\u5668\u3002\n\u5b83\u4f1a\u5728 elapsed \u5c5e\u6027\u4e2d\u8bb0\u5f55\u6574\u4e2a\u6d88\u8017\u65f6\u95f4\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u6765\u6f14\u793a\u600e\u6837\u4f7f\u7528\u5b83\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def countdown(n):\n    while n > 0:\n        n -= 1\n\n# Use 1: Explicit start/stop\nt = Timer()\nt.start()\ncountdown(1000000)\nt.stop()\nprint(t.elapsed)\n\n# Use 2: As a context manager\nwith t:\n    countdown(1000000)\n\nprint(t.elapsed)\n\nwith Timer() as t2:\n    countdown(1000000)\nprint(t2.elapsed)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u5b9e\u7528\u7684\u7c7b\u6765\u5b9e\u73b0\u65f6\u95f4\u8bb0\u5f55\u4ee5\u53ca\u8017\u65f6\u8ba1\u7b97\u3002\n\u540c\u65f6\u4e5f\u662f\u5bf9\u4f7f\u7528with\u8bed\u53e5\u4ee5\u53ca\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u534f\u8bae\u7684\u4e00\u4e2a\u5f88\u597d\u7684\u6f14\u793a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8ba1\u65f6\u4e2d\u8981\u8003\u8651\u4e00\u4e2a\u5e95\u5c42\u7684\u65f6\u95f4\u51fd\u6570\u95ee\u9898\u3002\u4e00\u822c\u6765\u8bf4\uff0c\n\u4f7f\u7528 time.time() \u6216 time.clock() \u8ba1\u7b97\u7684\u65f6\u95f4\u7cbe\u5ea6\u56e0\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0d\u540c\u4f1a\u6709\u6240\u4e0d\u540c\u3002\n\u800c\u4f7f\u7528 time.perf_counter() \u51fd\u6570\u53ef\u4ee5\u786e\u4fdd\u4f7f\u7528\u7cfb\u7edf\u4e0a\u9762\u6700\u7cbe\u786e\u7684\u8ba1\u65f6\u5668\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u7531 Timer \u7c7b\u8bb0\u5f55\u7684\u65f6\u95f4\u662f\u949f\u8868\u65f6\u95f4\uff0c\u5e76\u5305\u542b\u4e86\u6240\u6709\u4f11\u7720\u65f6\u95f4\u3002\n\u5982\u679c\u4f60\u53ea\u60f3\u8ba1\u7b97\u8be5\u8fdb\u7a0b\u6240\u82b1\u8d39\u7684CPU\u65f6\u95f4\uff0c\u5e94\u8be5\u4f7f\u7528 time.process_time() \u6765\u4ee3\u66ff\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "t = Timer(time.process_time)\nwith t:\n    countdown(1000000)\nprint(t.elapsed)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "time.perf_counter() \u548c time.process_time() \u90fd\u4f1a\u8fd4\u56de\u5c0f\u6570\u5f62\u5f0f\u7684\u79d2\u6570\u65f6\u95f4\u3002\n\u5b9e\u9645\u7684\u65f6\u95f4\u503c\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u4e3a\u4e86\u5f97\u5230\u6709\u610f\u4e49\u7684\u7ed3\u679c\uff0c\u4f60\u5f97\u6267\u884c\u4e24\u6b21\u51fd\u6570\u7136\u540e\u8ba1\u7b97\u5b83\u4eec\u7684\u5dee\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u66f4\u591a\u5173\u4e8e\u8ba1\u65f6\u548c\u6027\u80fd\u5206\u6790\u7684\u4f8b\u5b50\u8bf7\u53c2\u800314.13\u5c0f\u8282\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}